package com.isoftstone.bank.service.impl;


import com.isoftstone.bank.dao.BOrgMapper;
import com.isoftstone.bank.entity.BOrg;
import com.isoftstone.bank.service.IBOrgService;
import com.isoftstone.util.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by 文俊 on 2017/11/29.
 */
@Service
public class BOrgService implements IBOrgService {

    @Autowired
    BOrgMapper mapper;
    private List<BOrg> all;
    @Override
    public List<TreeNode> selectOrgTreeNodeBySupbak(Long supbak,boolean first){
        if(first){
            all = mapper.selectAll();
        }
        List<BOrg> list = this.selectBySupbak(supbak);
        if(list.isEmpty()){
            return null;
        }
        List<TreeNode> treeNodeList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            BOrg org = list.get(i);
            TreeNode node = new TreeNode();
            node.setId(org.getOrgId()+"");
            node.setLabel(org.getOrgName());
            node.setLevel(org.getOrgLevel());
            node.setChildren(this.selectOrgTreeNodeBySupbak(org.getOrgId(),false));
            node.setLeaf(node.getChildren() == null);
            node.setSupbak(org.getSupbak()+"");
            treeNodeList.add(node);
        }
        return treeNodeList;
    }
    @Override
    public List<TreeNode> selectOrgTreeNodeByOrgId(Long orgId) {
        if(all == null){
            all = mapper.selectAll();
        }
        List<TreeNode> treeNodeList = new ArrayList<>();
        BOrg org = all.stream().filter(o -> o.getOrgId().equals(orgId)).findFirst().get();
        TreeNode node = new TreeNode();
        node.setId(org.getOrgId()+"");
        node.setLabel(org.getOrgName());
        node.setLevel(org.getOrgLevel());
        node.setChildren(this.selectOrgTreeNodeBySupbak(org.getOrgId(),false));
        node.setLeaf(node.getChildren() == null);
        node.setSupbak(org.getSupbak()+"");
        treeNodeList.add(node);
        return treeNodeList;
    }

    @Override
    public List<TreeNode> selectMoveOrgTree(Long supbak, Long orgId) {
        if(all == null){
            all = mapper.selectAll();
        }
        List<BOrg> list = this.selectBySupbak(supbak);
        if(list.isEmpty()){
            return null;
        }
        List<TreeNode> treeNodeList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            BOrg org = list.get(i);
            //只能保存最多四级机构，所以移动选择树去掉所有四级机构和要移动的节点本身
            if(Integer.valueOf(org.getOrgLevel()) > 3 || org.getOrgId().equals(orgId)){
                continue;
            }
            TreeNode node = new TreeNode();
            node.setId(org.getOrgId()+"");
            node.setLabel(org.getOrgName());
            node.setLevel(org.getOrgLevel());
            node.setChildren(this.selectMoveOrgTree(org.getOrgId(),orgId));
            node.setLeaf(node.getChildren() == null);
            node.setSupbak(org.getSupbak()+"");
            treeNodeList.add(node);
        }
        return treeNodeList;
    }

    private List<BOrg> selectBySupbak(Long supbak) {
        List<BOrg> children = all.stream().filter(org -> org.getSupbak().equals(supbak)).collect(Collectors.toList());
        return children;
    }

    @Override
    public List<BOrg> exportExcel(){
        return mapper.exportExcel();
    }

    @Override
    public BOrg selectByPrimaryKey(Long orgId) {
        return mapper.selectByPrimaryKey(orgId);
    }

    @Override
    public boolean orgNameIsExsit(String orgName,Long orgid) {
        return mapper.orgNameIsExsit(orgName,orgid) != null;
    }



    @Override
    public int insertSelective(BOrg record) {
        return mapper.insertSelective(record);
    }

    @Override
    public int updateByPrimaryKeySelective(BOrg record) {
        return mapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int deleteByPrimaryKey(Long adId) {
        return mapper.deleteByPrimaryKey(adId);
    }

    @Override
    public Long selectNextOrgId() {
        return mapper.selectNextOrgId();
    }
}
